package com.example.mytest6;

import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;

import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.data.PieEntry;
import com.github.mikephil.charting.formatter.IValueFormatter;
import com.github.mikephil.charting.formatter.ValueFormatter;
import com.github.mikephil.charting.utils.ViewPortHandler;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

//zm
public class MyFragment1 extends Fragment {

    private PieChart pie1;
    private String TAG = "MyFragment1";

    public MyFragment1() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.myfragment1, container, false);
        initView(view);
        initData();
        return view;
    }


    private void initView(View view) {
        pie1 = view.findViewById(R.id.pic_chart);
    }

    private void initData() {

        //右下角文字说明
        pie1.setDescription(null);

        //形状为环形，默认true环形
        pie1.setDrawHoleEnabled(false);

        //能否转动 默认true能
        pie1.setRotationEnabled(false);

        //设置图中图例标签的颜色
        pie1.setEntryLabelColor(Color.BLACK);


        //图例  文字和图例大小，
        Legend legend = pie1.getLegend();
        //图例颜色大小
        legend.setFormSize(15);
        //图例文字大小
        legend.setTextSize(15);
        //图例位置
        legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);
        legend.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);
//        legend.setPosition(Legend.LegendPosition.BELOW_CHART_CENTER);

        //饼图数据大小设定，文字补充
        List<PieEntry> Value = new ArrayList<>();
        Value.add(new PieEntry(28.66f, "有违章"));
        Value.add(new PieEntry(71.33f, "无违章"));

        //数据输入
        PieDataSet dataSet = new PieDataSet(Value, "");

        //饼状图各部分之间的缝隙
        dataSet.setSliceSpace(1);

        //饼状图数据标签的指示线起始位置
        //getHoleRadius 这个是一开始的环形内圆的半径
        //且这个内圆半径默认为整个大圆的50%
        dataSet.setValueLinePart1OffsetPercentage(pie1.getHoleRadius() * 2);
        //斜着的线的长度
        dataSet.setValueLinePart1Length(0.7f);
        //指向数值的线的长度
        dataSet.setValueLinePart2Length(0.2f);


        //设置颜色
        int colors[] = {Color.BLUE, Color.RED};
        dataSet.setColors(colors);

        //数据标签中 yV.get(0).getValue() 大小   即数据的大小
        dataSet.setValueTextSize(20);


        dataSet.setValueFormatter(new PieDataValueFormatter());

        //设置数据标签  在饼状图里 还是 饼状图外面
        //x是文字 y是数值
        dataSet.setXValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE);
        dataSet.setYValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE);

        pie1.setData(new PieData(dataSet));
    }

    class PieDataValueFormatter extends ValueFormatter {
        DecimalFormat format = new DecimalFormat("0.00%");

        @Override
        public String getPieLabel(float value, PieEntry pieEntry) {
            return format.format(value / 100);
        }
    }


}
